package com.mk.rxjavademo;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * http://gank.io/post/560e15be2dca930e00da1083
 */

public class MainActivity extends AppCompatActivity {

    private String tag = "rxJava";
    private ImageView mImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImg = (ImageView) findViewById(R.id.img);
    }


    public void testRx(View v) {


//        observable.subscribe(subscriber);

/*        Observable just = Observable.just("Hello", "哈哈", "四十四");
        // 将会依次调用：
       // onNext("Hello");
       // onNext("Hi");
       // onNext("Aloha");
       // onCompleted();
        just.subscribe(subscriber);


        String[] words = {"Hello", "Hi", "Aloha"};
        Observable observable = Observable.from(words);
// 将会依次调用：
// onNext("Hello");
// onNext("Hi");
// onNext("Aloha");
// onCompleted();*/

//        printStringArray();
//        setPicToImg();

//        ChangeThreadModed();

        RxJavaMapTest();
    }


    //Observer 也总是会先被转换成一个 Subscriber 再使用
    Subscriber<String> subscriber = new Subscriber<String>() {
        @Override
        public void onNext(String s) {
            Log.d(tag, "Item: " + s);
        }

        @Override
        public void onCompleted() {
            Log.d(tag, "Completed!");
        }

        @Override
        public void onError(Throwable e) {
            Log.d(tag, "Error!");
        }
    };


    //被观察者
    Observable observable = Observable.create(new Observable.OnSubscribe<String>() {
        @Override
        public void call(Subscriber<? super String> subscriber) {
            subscriber.onNext("Hello");
            subscriber.onNext("Hi");
            subscriber.onNext("Aloha");
            subscriber.onCompleted();
        }
    });

// 还支持不完整定义的回调，RxJava 会自动根据定义创建出 Subscriber
    public void test2() {

        Action1<String> onNextAction = new Action1<String>() {
            // onNext()
            @Override
            public void call(String s) {
                Log.d(tag, s);
            }
        };
        Action1<Throwable> onErrorAction = new Action1<Throwable>() {
            // onError()
            @Override
            public void call(Throwable throwable) {
                // Error handling
            }
        };
        Action0 onCompletedAction = new Action0() {
            // onCompleted()
            @Override
            public void call() {
                Log.d(tag, "completed");
            }
        };

// 自动创建 Subscriber ，并使用 onNextAction 来定义 onNext()
        observable.subscribe(onNextAction);
// 自动创建 Subscriber ，并使用 onNextAction 和 onErrorAction 来定义 onNext() 和 onError()
        observable.subscribe(onNextAction, onErrorAction);
// 自动创建 Subscriber ，并使用 onNextAction、 onErrorAction 和 onCompletedAction 来定义 onNext()、 onError() 和 onCompleted()
        observable.subscribe(onNextAction, onErrorAction, onCompletedAction);
    }



    public void printStringArray(){
        String[]  names={"1","2","3"};

        Observable.from(names)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        Log.d(tag, "str: " + s);
                    }
                });
    }

    /**
     * RxJava调度设置图片
     */
    public void setPicToImg(){
        final int drawableId=R.mipmap.ic_launcher;
        Observable.create(new Observable.OnSubscribe<Drawable>() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void call(Subscriber<? super Drawable> subscriber) {
                Drawable drawable = getTheme().getDrawable(drawableId);
                subscriber.onNext(drawable);
                subscriber.onCompleted();

            }
        }).subscribe(new Observer<Drawable>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Drawable drawable) {
                mImg.setImageDrawable(drawable);
            }
        });
    }


    /**
     * 切换线程的模式  『后台线程取数据，主线程显示』
     */
    public void  ChangeThreadModed(){

        Observable.just(1,2,3,4)
                .subscribeOn(Schedulers.io())//事件产生的线程
                .observeOn(AndroidSchedulers.mainThread())//消费的线程。
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.d(tag,"number:"+integer);
                    }
                });

    }







    /*******************************************变换  核心*******************************************/

    /**
     * Map 转换对象  神器
     */
    public void RxJavaMapTest(){

        Observable.just(R.mipmap.ic_launcher)
                .map(new Func1<Integer, Drawable>() {
                    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                    @Override
                    public Drawable call(Integer s) {
                        Drawable drawable = getTheme().getDrawable(s);
                        return drawable;
                    }
                }).subscribe(new Action1<Drawable>() {
            @Override
            public void call(Drawable bitmap) {
                mImg.setImageDrawable(bitmap);
            }
        });
    }

//批量转换
    public void  RxJavaFlatMapTest(){
//        Student[] students = ...;
//        Subscriber<Course> subscriber = new Subscriber<Course>() {
//            @Override
//            public void onNext(Course course) {
//                Log.d(tag, course.getName());
//            }
//            ...
//        };
//        Observable.from(students)
//                .flatMap(new Func1<Student, Observable<Course>>() {
//                    @Override
//                    public Observable<Course> call(Student student) {
//                        return Observable.from(student.getCourses());
//                    }
//                })
//                .subscribe(subscriber);


    }








}
